<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info"
                 @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边导航栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠按钮 -->
        <div class="toggle-button"
             @click="changeCollapse">|||</div>
        <el-menu background-color="#363c42"
                 text-color="#fff"
                 active-text-color="#09a"
                 unique-opened
                 :collapse="isCollapse"
                 :collapse-transition="false"
                 :default-active="aPath"
                 router>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id.toString()"
                      v-for="item in menuList"
                      :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="chooseActivePath(subItem.path)">
              <template slot="title">
                <i class="el-icon-cold-drink"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created () {
    // 请求 菜单数据
    this.getMenuList()
    // 根据 本地缓存中的 aPath 手动显示 高亮菜单
    this.aPath = window.sessionStorage.getItem('aPath')
  },
  data () {
    return {
      // 当前激活菜单的path
      aPath: '',
      // 菜单是否折叠
      isCollapse: false,
      // 菜单数组
      menuList: [],
      // 一级菜单字体图标样式
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  methods: {
    // 退出方法
    logout () {
      this.$messageBox.confirm('确认退出吗?', '提示信息', {
        confirmButtonText: '确定',
        callback: action => {
          if (action === 'confirm') {
            // 删除 sessionStorage
            window.sessionStorage.clear()
            // 导航到 登录页面
            this.$router.push('/login')
          }
        }
      })
    },
    async getMenuList () {
      const { data: resData } = await this.$http.get('menus')
      // 获取菜单成功
      if (resData.meta.status === 200) {
        // 将返回的 菜单数组 设置给 vue实例的 data.menulist
        this.menuList = resData.data
      }
    },
    // 修改菜单折叠状态
    changeCollapse () {
      this.isCollapse = !this.isCollapse
    },
    chooseActivePath (aPath) {
      // 通过 设置给 data中的 aPath来修改 菜单的 default-active属性来 将被点击的菜单高亮
      this.aPath = aPath
      window.sessionStorage.setItem('aPath', aPath)
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #363c42;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #e9edf1;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
